﻿using System;
using System.Configuration;

namespace QuailDashboard.View.Web
{
    /// <summary>
    /// The dashboard page
    /// </summary>
    public partial class Dashboard : System.Web.UI.Page
    {
        public string WindowLess { get; set; }

        /// <summary>
        /// Gets or sets the user's name
        /// </summary>
        public string UserName
        {
            get;
            private set;
        }

        /// <summary>
        /// Gets or sets the QuailDataServiceAddress
        /// </summary>
        public string QuailDataServiceAddress
        {
            get; 
            private set;
        }

        /// <summary>
        /// Gets or sets the test runner service address.
        /// </summary>
        public string TestRunnerServiceAddress
        {
            get;
            private set;
        }

        /// <summary>
        /// Gets or sets a value indicating whether to enable test execution
        /// </summary>
        public bool EnableTestExecute
        {
            get;
            private set;
        }

        /// <summary>
        /// Called on page load
        /// </summary>
        /// <param name="sender">the page instance</param>
        /// <param name="e">not used by method</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            WindowLess = (String.IsNullOrEmpty(Request.QueryString["WindowLess"])) ? "true" : Request.QueryString["WindowLess"];

            QuailDataServiceAddress = FixUrl("/QuailDataService.svc");
            TestRunnerServiceAddress = FixUrl("/QuailTestRunner.svc");

            if (User != null && User.Identity != null && User.Identity.IsAuthenticated)
                UserName = User.Identity.Name;

            EnableTestExecute = ConfigurationManager.AppSettings["EnableTestExecute"] == "true";
        }

        private string FixUrl(string url)
        {
            var newurl = Request.Url.Scheme + "://" + Request.Url.Host;

            if (!Request.Url.IsDefaultPort)
                newurl += ":" + Request.Url.Port;

            newurl += url;

            return newurl;
        }
    }
}
